package com.wangtao.firstapp.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.modles.CategoryShowDetailsComments;
import com.wangtao.firstapp.modles.CategoryShowDetailsData;
import com.wangtao.firstapp.utils.HttpUtils;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 晒单详情页面
 * Created by lanouhn on 16/9/7.
 */
public class CategoryShowDetailsActivity extends AppCompatActivity {
    private CircleImageView civ_header_categoryshowdetails;// 作者头像
    private TextView tv_author_categoryshowdetails;// 作者昵称
    private TextView tv_datestr_categoryshowdetails;// 时间
    private ImageView iv_bigpic_categoryshowdetails;// 商品大图
    private ImageView iv_smallpic_categoryshowdetails;// 商品小图
    private TextView tv_productname_categoryshowdetails;// 商品名称
    private TextView tv_price_categoryshowdetails;// 商品价格
    private TextView tv_peoplelikes_categoryshowdetails;// 喜欢人数
    private TextView tv_comments_categoryshowdetails;// 评论数
    private ImageView iv_back_titlebar_categoryshowdetails;// 标题左上角返回按钮
    private LinearLayout linearLayout_categoryshowdetails;// 加载正文
    private LinearLayout linearlayout_tags;// 标签
    private LinearLayout linearlayout_comments;// 加载评论
    private CategoryShowDetailsData cshowdetailsdata;// 数据实体类
    private CategoryShowDetailsComments categoryShowDetailsComments;// 评论实体类

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_categoryshowdetails);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        civ_header_categoryshowdetails = (CircleImageView) findViewById(R.id.civ_header_categoryshowdetails);
        tv_author_categoryshowdetails = (TextView) findViewById(R.id.tv_author_categoryshowdetails);
        tv_datestr_categoryshowdetails = (TextView) findViewById(R.id.tv_datestr_categoryshowdetails);
        iv_bigpic_categoryshowdetails = (ImageView) findViewById(R.id.iv_bigpic_categoryshowdetails);
        iv_smallpic_categoryshowdetails = (ImageView) findViewById(R.id.iv_smallpic_categoryshowdetails);
        tv_productname_categoryshowdetails = (TextView) findViewById(R.id.tv_productname_categoryshowdetails);
        tv_price_categoryshowdetails = (TextView) findViewById(R.id.tv_price_categoryshowdetails);
        tv_peoplelikes_categoryshowdetails = (TextView) findViewById(R.id.tv_peoplelikes_categoryshowdetails);
        tv_comments_categoryshowdetails = (TextView) findViewById(R.id.tv_comments_categoryshowdetails);
        iv_back_titlebar_categoryshowdetails = (ImageView) findViewById(R.id.iv_back_titlebar_categoryshowdetails);
        linearLayout_categoryshowdetails = (LinearLayout) findViewById(R.id.linearLayout_categoryshowdetails);
        linearlayout_comments = (LinearLayout) findViewById(R.id.linearlayout_comments);
        linearlayout_tags = (LinearLayout) findViewById(R.id.linearlayout_tags);
    }

    // 获取数据
    private void initData() {
        Intent intent = getIntent();
        String id = intent.getStringExtra("itemid");
        String url = "http://open4.bantangapp.com/community/post/info";
        String param = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2559956&oauth_token=6beffa8c92276d19d3c23acb596024e6&track_deviceid=866962021116243&track_device_info=m1+note&channel_name=meizu&app_installtime=1471923362&app_versions=5.8.7&os_versions=5.1&screensize=1080&v=19&id=" + id + "&trace_id=";
        try {
            HttpUtils.doPostAsyn(url, param, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    cshowdetailsdata = gson.fromJson(result, CategoryShowDetailsData.class);
                    if (cshowdetailsdata.getData() != null) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 获取评论
        String url1 = "http://open4.bantangapp.com/post/comment/list";
        String param1 = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2559956&oauth_token=6beffa8c92276d19d3c23acb596024e6&track_deviceid=866962021116243&track_device_info=m1+note&channel_name=meizu&app_installtime=1471923362&app_versions=5.8.7&os_versions=5.1&screensize=1080&v=19&page=0&pagesize=10&id=" + id;
        try {
            HttpUtils.doPostAsyn(url1, param1, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson1 = new Gson();
                    categoryShowDetailsComments = gson1.fromJson(result, CategoryShowDetailsComments.class);
                    if (categoryShowDetailsComments.getData().size() > 0) {
                        handler.sendEmptyMessage(2);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initEvent() {
        iv_back_titlebar_categoryshowdetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        linearLayout_categoryshowdetails.setPadding(20, 0, 20, 0);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    // Toast.makeText(CategoryShowDetailsActivity.this, "网络访问失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    // 加载标签
//                    for (int i = 0; i < cshowdetailsdata.getData().getPost().getTags().size(); i++) {
//                        TextView textView_tags = new TextView(CategoryShowDetailsActivity.this);
//                        textView_tags.setText(cshowdetailsdata.getData().getPost().getTags().get(i).getName());
//                        linearlayout_tags.addView(textView_tags);
//                    }

                    Picasso.with(CategoryShowDetailsActivity.this).load(cshowdetailsdata.getData().getPost().getAuthor().getAvatar()).config(Bitmap.Config.RGB_565).into(civ_header_categoryshowdetails);
                    tv_author_categoryshowdetails.setText(cshowdetailsdata.getData().getPost().getAuthor().getNickname());
                    tv_datestr_categoryshowdetails.setText(cshowdetailsdata.getData().getPost().getDatestr());
                    Picasso.with(CategoryShowDetailsActivity.this).load(cshowdetailsdata.getData().getPost().getMiddle_pic_url()).config(Bitmap.Config.RGB_565).into(iv_bigpic_categoryshowdetails);
                    tv_peoplelikes_categoryshowdetails.setText(cshowdetailsdata.getData().getPost().getDynamic().getLikes() + "人喜欢");

                    for (int i = 0; i < cshowdetailsdata.getData().getPost().getProduct().size(); i++) {
                        Picasso.with(CategoryShowDetailsActivity.this).load(cshowdetailsdata.getData().getPost().getProduct().get(i).getPic()).config(Bitmap.Config.RGB_565).into(iv_smallpic_categoryshowdetails);
                        tv_productname_categoryshowdetails.setText(cshowdetailsdata.getData().getPost().getProduct().get(i).getTitle());
                        tv_price_categoryshowdetails.setText("¥" + cshowdetailsdata.getData().getPost().getProduct().get(i).getPrice());
                    }

                    TextView textView_content = new TextView(CategoryShowDetailsActivity.this);
                    textView_content.setText(cshowdetailsdata.getData().getPost().getContent());
                    textView_content.setLineSpacing(1, 1.5f);
                    linearLayout_categoryshowdetails.addView(textView_content);

                    for (int i = 0; i < cshowdetailsdata.getData().getPost().getTags().size(); i++) {
                        TextView textView_tags = new TextView(CategoryShowDetailsActivity.this);
                        textView_tags.setGravity(Gravity.CENTER);
                        textView_tags.setPadding(5, 5, 5, 5);
                        textView_tags.setTextSize(10);
                        textView_tags.setTextColor(getResources().getColor(R.color.mycolor1));
                        textView_tags.setText(cshowdetailsdata.getData().getPost().getTags().get(i).getName());
                        linearlayout_tags.addView(textView_tags);
                    }
                    break;
                case 2:
                    tv_comments_categoryshowdetails.setText("所有" + categoryShowDetailsComments.getData().size() + "条评论");
                    for (int i = 0; i < categoryShowDetailsComments.getData().size(); i++) {
                        TextView textView_user = new TextView(CategoryShowDetailsActivity.this);
                        textView_user.setPadding(10, 0, 0, 0);
                        TextView textView_comments = new TextView(CategoryShowDetailsActivity.this);
                        textView_comments.setPadding(10, 0, 0, 0);

                        if (categoryShowDetailsComments.getData().get(i).getAt_user().getNickname() != null) {
                            String user = categoryShowDetailsComments.getData().get(i).getUser().getNickname() + ": ";

                            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(user);
                            spannableStringBuilder.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.mycolor)), 0, user.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            // textView_user.setText(spannableStringBuilder);

                            String atuser = categoryShowDetailsComments.getData().get(i).getAt_user().getNickname();
                            String content = "回复 @" + categoryShowDetailsComments.getData().get(i).getAt_user().getNickname() + " " + categoryShowDetailsComments.getData().get(i).getConent();
                            SpannableStringBuilder ssbuser = new SpannableStringBuilder(content);
                            ssbuser.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.mycolor)), 2, atuser.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                            textView_comments.setText(spannableStringBuilder.append(ssbuser));

                        } else {
                            String user = categoryShowDetailsComments.getData().get(i).getUser().getNickname() + ": ";
                            String content = user + categoryShowDetailsComments.getData().get(i).getConent();
                            SpannableStringBuilder ssbuser = new SpannableStringBuilder(content);
                            ssbuser.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.mycolor)), 0, user.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            textView_comments.setText(ssbuser);
                        }
                        linearlayout_comments.addView(textView_comments);
                    }
                    break;
            }
        }
    };

}
